package com.android.baseconfig.common.utils;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author lei
 * @description RxTimerUtil
 * @date 2018/5/19
 */
public class RxTimerUtil {
    private static Disposable mDisposable;
    private static Disposable countdownDisposable;
    private static Disposable intervalDisposable;

    /**
     * milliseconds毫秒后执行next操作
     *
     * @param milliseconds
     * @param next
     */
    public static void timer(long milliseconds, final IRxNext next) {
        Observable.timer(milliseconds, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mDisposable=disposable;
                    }

                    @Override
                    public void onNext(@NonNull Long number) {
                        if(next!=null){
                            next.doNext(number);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //取消订阅
                        cancel(mDisposable);
                    }

                    @Override
                    public void onComplete() {
                        //取消订阅
                        cancel(mDisposable);
                    }
                });
    }

    /**
     * 每隔milliseconds毫秒后执行next操作
     *
     * @param milliseconds
     * @param next
     */
    public static void interval(long milliseconds, final IRxNext next) {
        intervalDisposable = Flowable.interval(milliseconds, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(next::doNext)
                .subscribe();
    }

    /**
     * 每隔seconds秒后执行next操作
     *
     * @param milliseconds
     * @param next
     */
    public static void countdown(int count, final IRxNext next) {
        //intervalRange四个参数分别为：从0开始、到60结束、延时0开始，单位时间（NANOSECONDS,MICROSECONDS,MILLISECONDS,SECONDS,MINUTES,HOURS,DAYS）。
        countdownDisposable = Flowable.intervalRange(0, count, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> next.doNext(count - aLong))
                .doOnComplete(() -> {
                    //倒计时完毕事件处理
                    next.doNext(-1);
                })
                .subscribe();
    }

    /**
     * 取消订阅
     */
    public static void cancel() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            Logc.d("RxTimer", "====定时器取消======");
        }
    }

    /**
     * 取消订阅
     */
    public static void cancel(Disposable disposable) {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
            Logc.d("RxTimer", "====定时器取消======");
        }
    }

    /**
     * 取消订阅
     */
    public static void cancelCountdown() {
        if (countdownDisposable != null && !countdownDisposable.isDisposed()) {
            countdownDisposable.dispose();
            Logc.d("RxTimer", "====倒计时取消======");
        }
    }

    /**
     * 取消定时
     */
    public static void cancelInterval() {
        if (intervalDisposable != null && !intervalDisposable.isDisposed()) {
            intervalDisposable.dispose();
            Logc.d("RxTimer", "====定时取消======");
        }
    }

    public interface IRxNext {
        void doNext(long number);
    }
}
